﻿using Materal.Extensions;
using Microsoft.Extensions.Hosting;
using System.Diagnostics;

namespace ConsoleTest.Services
{
    public class ActivityService : IHostedService
    {
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            Activity activity = new("MyActivity")
            {
                DisplayName = "我的活动",
            };
            activity.Start();
            activity.AddBaggage("Test", "Hello World!");
            Test();
            activity.Stop();
            await Task.CompletedTask;
        }
        public async Task StopAsync(CancellationToken cancellationToken) => await Task.CompletedTask;
        private static void Test()
        {
            Activity? activity = Activity.Current;
            if (activity is null) return;
            Console.WriteLine($"Activity Name: {activity.OperationName}");
            Console.WriteLine($"Activity DisplayName: {activity.DisplayName}");
            foreach (KeyValuePair<string, string?> item in activity.Baggage)
            {
                Console.WriteLine($"Baggage {item.Key} : {item.Value}");
            }
        }
    }
}
